package com.testing.redboy.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.testing.redboy.JSActivity;
import com.testing.redboy.R;
import com.testing.redboy.adapter.MybsA;
import com.testing.redboy.bean.DbInfo;
import com.testing.redboy.db.InitDb;
import com.testing.redboy.interfaces.CallBackA;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;


public class ShoppingFragment extends Fragment implements CallBackA {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private ListView lv;
    private CheckBox pickAll;
    private TextView total_price;
    private TextView total_num;
    private DbManager db;
    private List<DbInfo> all;
    private MybsA mybsA;
    private int numx;
    private int pricex;
    private TextView jiesuan;

    public ShoppingFragment() {
    }

    public static ShoppingFragment newInstance(String param1, String param2) {
        ShoppingFragment fragment = new ShoppingFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        InitDb initDb = new InitDb();
        DbManager.DaoConfig config = initDb.init();
        db = x.getDb(config);

        View v = inflater.inflate(R.layout.fragment_shopping, container, false);

        //初始化控件
        initView(v);

        //从数据库获取数据
        all = getData();

        //listview适配
        mybsA = new MybsA(getActivity(), all);
        lv.setAdapter(mybsA);

        mybsA.setCallBackA(this);


        pickAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean ooo = pickAll.isChecked();
                int numx2 = 0;
                int pricex2 = 0;
                if (ooo) {
                    for (DbInfo dd : all) {
                        dd.setPick(true);
                        String num2_ = dd.getNum();
                        int num2 = Integer.parseInt(num2_);
                        numx2 = numx2 + num2;
                        mybsA.notifyDataSetChanged();
                    }
                    pricex2 = numx2 * 100;
                    pricex = pricex2;
                    numx = numx2;
                    total_price.setText("合计:" + pricex2 + "元");
                    total_num.setText("已选择:" + numx2 + "件");
                } else {
                    for (DbInfo dd2 : all) {
                        dd2.setPick(false);
                        mybsA.notifyDataSetChanged();
                    }
                    pricex = pricex2;
                    numx = numx2;
                    total_price.setText("合计:" + pricex2 + "元");
                    total_num.setText("已选择:" + numx2 + "件");
                }
            }
        });


        jiesuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), JSActivity.class);
                intent.putExtra("zongshu", numx);
                intent.putExtra("zongjia", pricex);
                startActivity(intent);
            }
        });

        return v;
    }


    @Override
    public void keepChange(int id, int z) {
        //    Log.d("ZZZ","leng"+all.get(id).getNum());
        // Log.d("zzz","**************"+all.get(id).isPick());

//        try {
//            DbInfo byId = db.findById(DbInfo.class, id + 1);
//            byId.setNum(all.get(id).getNum()+"");
//            byId.setPick(all.get(id).isPick());
//            db.update(byId,"num");
//            db.update(byId,"isPick");
//        } catch (DbException e) {
//            e.printStackTrace();
//        }


        //循环判断
        int b = 0;
        for (DbInfo dd3 : all) {
            boolean pick = dd3.isPick();
            if (pick) {
                b++;
            }
        }

        if (b == all.size()) {
            pickAll.setChecked(true);
        } else {
            pickAll.setChecked(false);
        }

//        try {
//            DbInfo byId2 = db.findById(DbInfo.class, id + 1);
//            boolean pick = byId2.isPick();
//
//            if(pick){
//                String num_ = byId2.getNum();
//                int num = Integer.parseInt(num_);
//                int price = byId2.getPrice();
//
//                int i = num * price;
//
//                Log.d("zzz",i+"************************");
//                Log.d("zzz",num+"////////////////////////");
//            }
//        } catch (DbException e) {
//            e.printStackTrace();
//        }
//
//        total_num.setText("已选择"+numx+"件商品");
//        total_price.setText("合计:"+pricex+"元");

        //获取数量

        //       DbInfo byId2 = db.findById(DbInfo.class, id + 1);
        DbInfo dd = all.get(id);
        String num_ = dd.getNum();
        int num = Integer.parseInt(num_);
        int price = dd.getPrice();


        //       Log.d("zzz",num+"***"+z);

        if (z == 0) {
            numx = numx - num;
            pricex = price * numx;
        } else if (z == 1) {
            numx = numx + num;
            pricex = price * numx;
        }

        if (z == 2) {
            ++numx;
            Log.d("zzz", "numx=" + numx);
            pricex = price * (numx);
        } else if (z == 3) {
            if (num >= 0) {
                --numx;
                pricex = price * (numx);
            }
        }


        total_price.setText("合计:" + pricex + "元");
        total_num.setText("已选择:" + numx + "件");


    }


    private void initView(View v) {
        lv = (ListView) v.findViewById(R.id.lv);
        pickAll = (CheckBox) v.findViewById(R.id.pickAll);
        total_price = (TextView) v.findViewById(R.id.total_price);
        total_num = (TextView) v.findViewById(R.id.total_num);
        jiesuan = (TextView) v.findViewById(R.id.jiesuan);
    }

    public List<DbInfo> getData() {
        List<DbInfo> all = null;
        try {
            all = db.findAll(DbInfo.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return all;
    }
}
